/**
* Copyright @ 2023 iAuto (Shanghai) Co., Ltd.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are NOT permitted except as agreed by
* iAuto (Shanghai) Co., Ltd.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/



import { getDateTime } from '@/utils/soa/utils';
import { useRequest } from 'ahooks';
import { Col, Descriptions, Image, message, Row, Space } from 'antd';
import IAutoDetail from '@/components/soa/IAutoDetail';
import React from 'react';
import styles from './detail.less';
import {getScenesDetail, removeScenes, getScenesList1, checkAudit} from '@/services/soa/sceneOfficial';
import { router } from 'umi';
import IAutoShowWarnModal from '@/components/soa/IAutoShowWarnModal';
import IAutoButton from '@/components/soa/IAutoButton';

// TODO: 类型
const Index = ({ location }: any) => {
  const { query,pathname } = location;
  // 请求回来的参数
  const { data, loading: requestLoading } = useRequest(
    async (sceneUuid: string) => {
      return getScenesDetail(sceneUuid);
    },
    {
      defaultParams: [query?.sceneUuid],
    },
  );
  // 删除
  const removeVehicleScenesRes = useRequest(removeScenes,
    {
      defaultParams: [query?.sceneUuid],
      manual: true,
      onSuccess: (result) => {
        if (result?.statusCode=='0') {
          message.success('删除成功！');
          router.push('/soaManage/scene/official');
        }
      },
    },
  );

  // 发布历史
  const releaseListRes = useRequest(getScenesList1, {
    defaultParams: [{ sceneUuid: query?.sceneUuid }],
  });

  //发布判断
  const checkAudits = useRequest(checkAudit, {
    manual:true,
    onSuccess:(res)=>{
      if(res?.data?.exist==true){
        // setCheck(1)
        message.warning("场景已在分享审核中，暂时无法分享!")
      }else {
        router.push('/soaManage/scene/official/release?id=' + query?.sceneUuid);
      }
    }
  });
  /**
   * @description 删除
   */
  const removeHandle = () => {
    IAutoShowWarnModal(
      `请确认是否删除${data?.data?.name}?`,
      () => {
        removeVehicleScenesRes.run(query?.sceneUuid);
      }
    );
  };

  /**
   * @description 发布
   */
  const release = (param: any) => {
    checkAudits.run(param?.sceneUuid)
  };

  /**
   * @description 发布履历
   */
  const releaseHistory = (flag: boolean, param: any) => {
    if (flag) {
      message.warning('无已发布场景！');
      return;
    } else {
      if (param) {
        router.push(
          // `/soaManage/scene/releaseHistory?id=${param?.sceneUuid}&name=${param?.cnName}`,
          `/soaManage/scene/official/releaseHistory?id=${param?.sceneUuid}&name=${param?.name}`,
        );
      } else {
        message.error('当前场景异常！');
      }
    }
  };

  return (
    <IAutoDetail className={styles.main} title={'场景详情'} loading={requestLoading}>
      <Row>
        <Col span={24} className={styles.infoWrap}>
          <Row>
            <Col flex={'none'}>
              <Image
                src={data?.data?.backgroundPicUrl}
                preview={false}
                className={styles.infoImage}
              />
            </Col >
            <Col flex={'auto'} className={styles.info} span={18}>
              <Row>
                <Col span={24}>
                  <Row align={'middle'}>
                    <Col flex={'none'}>
                      <Image
                        src={data?.data?.iconUrl}
                        preview={false}
                        className={styles.iconImage}
                      />
                    </Col>
                    {/*<Col>*/}
                    {/*  <span className={styles.title}>{data?.data?.name}</span>*/}
                    {/*</Col>*/}
                    <Col flex={'auto'}>
                      <Row justify={'end'}>
                        <Col>
                          <Space>
                            <IAutoButton
                              buttonType={"form"}
                              type={"primary"}
                              size={"normal"}
                              pathname={pathname}
                              auth={"officialDetail_doHistory"}
                              onClick={() =>
                                releaseHistory(releaseListRes?.data?.list?.length === 0, data?.data)
                              }
                            >
                              发布历史
                            </IAutoButton>
                            <IAutoButton
                              buttonType={"form"}
                              type={"primary"}
                              size={"normal"}
                              pathname={pathname}
                              auth={"officialDetail_doCreate"}
                              onClick={() =>
                                // ${123}
                                (window.location.href = `/#/soaManage/scene-editing?sceneUuid=${
                                  query?.sceneUuid
                                }&cnName=${data?.data?.name}&seriesCode=${
                                  data?.data?.seriesCode
                                }&modelCode=${data?.data?.modelCode}`)
                              }
                            >
                              场景编排
                            </IAutoButton>
                            <IAutoButton
                              buttonType={"form"}
                              type={"primary"}
                              size={"normal"}
                              pathname={pathname}
                              auth={"officialDetail_doEdit"}
                              onClick={() =>
                                router.push(
                                  '/soaManage/scene/official/edit?sceneUuid=' +
                                    query?.sceneUuid,
                                )
                              }
                            >
                              编辑
                            </IAutoButton>
                            <IAutoButton
                              buttonType={"form"}
                              type={"primary"}
                              size={"normal"}
                              pathname={pathname}
                              auth={"officialDetail_doPublish"}
                              onClick={() => release(data?.data)}
                            >
                              发布
                            </IAutoButton>
                            <IAutoButton
                              buttonType={"form"}
                              size={"normal"}
                              type={undefined}
                              pathname={pathname}
                              auth={"officialDetail_doDelete"}
                              onClick={removeHandle}>删除</IAutoButton>
                          </Space>
                        </Col>
                      </Row>
                    </Col>
                  </Row>
                </Col>
              </Row>
              <Row style={{marginTop:14}}>
                <Col>
                  <span className={styles.title}>{data?.data?.name}</span>
                </Col>
              </Row>
              <Row>
                <Col style={{ marginTop: 14,fontSize:14 }}>
                  <span>{data?.data?.description}</span>
                </Col>
              </Row>
            </Col>
          </Row>
        </Col>
      </Row>

      <Row className={styles.baseInfo}>
        <Col>
          <span>基本信息</span>
        </Col>
      </Row>

      <Row>
        <Col span={24} className={styles.description}>
          <Descriptions column={1}  contentStyle={{whiteSpace: 'normal'}}>
            <Descriptions.Item label={'品牌'}>{(data?.data?.brandName)}</Descriptions.Item>
            <Descriptions.Item label={'车系'}>{(data?.data?.seriesName)}</Descriptions.Item>
            <Descriptions.Item label={'车型'}>
              {(data?.data?.modelName)}
            </Descriptions.Item>
            <Descriptions.Item label={'场景分类'}>{data?.data?.categoryName}</Descriptions.Item>
            <Descriptions.Item label={'是否官方场景'}>{data?.data?.officialScene==0?"否":"是"}</Descriptions.Item>
            <Descriptions.Item label={'手机端可执行'}>{data?.data?.executedOnPhone==0?"否":"是"}</Descriptions.Item>
            {
              data?.data?.haveStartTrigger === 1 &&(
                <Descriptions.Item label={'车机端二次确认'}>
                  {data?.data?.doubleCheckOnVehicle==0?"否":"是"}
                </Descriptions.Item>
              )
            }
            {
              data?.data?.haveStartTrigger === 1 &&(
                <Descriptions.Item label={'周期内执行次数'}>
                  {data?.data?.cycleExecution==1?"单次":"无限制"}
                </Descriptions.Item>
              )
            }
            <Descriptions.Item label={'创建人'}>{data?.data?.developerName}</Descriptions.Item>
            <Descriptions.Item label={'更新时间'}>{getDateTime(data?.data?.updateTime)}</Descriptions.Item>
          </Descriptions>
        </Col>
      </Row>
    </IAutoDetail>
  );
};

export default Index;
